﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

using CE.Mail;
using CE.User;
using CE.Database;
using CE.TravelInsurance;
using CE.Global;
using CE.Provider;
using CE.AreaCountry;

public partial class travel_redirecttocx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["mtaid"] != null && Request.QueryString["mpid"] != null
                && Request.QueryString["muid"] != null && Request.QueryString["mtarid"] != null
                && Request.QueryString["mpremium"] != null && Request.QueryString["mplan"] != null)
            {
                Session["TAFromSummary"] = "1";
                TAUserOptionLog(true, false, false);

                Int64 _UserId = 0;
                if (Session[General.UserId] != null)
                {
                    _UserId = Convert.ToInt64(Session[General.UserId].ToString());
                }

                string CoverType = "";

                if (Session["PlanType"] != null)
                    CoverType = Session["PlanType"].ToString();

                string PlanName = Request.QueryString["mplan"].ToString();
                string ProviderID = Request.QueryString["mpid"].ToString();

                Int64 TAId = Convert.ToInt64(Request.QueryString["mtaid"].ToString());
                Int64 _resultId = Convert.ToInt64(Request.QueryString["mtarid"].ToString());

                float TotalPremium = 0;
                TotalPremium = float.Parse(Request.QueryString["mpremium"].ToString());

                if (Session[General.TAId] != null)
                    TAId = Convert.ToInt64(Session[General.TAId]);

                LogUserSelectHistory(CoverType, _resultId, Convert.ToInt64(ProviderID));

                //update total premium field
                TAApplicantSearchDB _taapplicantsearchdb = new TAApplicantSearchDB();
                TAApplicantSearchDB _taapplicantsearchdbBuy = new TAApplicantSearchDB();

                Int64 UpdatedId = _taapplicantsearchdbBuy.UpdatePremium(TAId, TotalPremium, _resultId, Convert.ToInt64(ProviderID), PlanName);
            

                RedirectTo(Request.QueryString["mpid"].ToString(), Request.QueryString["mtaid"].ToString(),
                    Request.QueryString["muid"].ToString(), Request.QueryString["mtarid"].ToString());
            }
            else
            {
                Response.Redirect("travel-quote.aspx");
            }


            
        }
    }

    private void RedirectTo(string _companyId, string _taid, string _userid, string _resultid)
    {
        string _planType = "";

        if (Session["PlanType"] != null)
            _planType = Session["PlanType"].ToString();


        if (_companyId == "29") {
            string _formURL = CE.Configuration.MyConfigurationManager.GetAppSettingValue("Mobile.LibertyForm");

            Response.Redirect(_formURL + "?mtaid=" + _taid + "&mpid=" + _companyId + "&muid=" + _userid + "&mtarid=" + _resultid
                + "&mplan=" + _planType);
        }
        else if (_companyId == "30") {
            string _formURL = CE.Configuration.MyConfigurationManager.GetAppSettingValue("Mobile.EtiqaForm");

            Response.Redirect(_formURL + "?mtaid=" + _taid + "&mpid=" + _companyId + "&muid=" + _userid + "&mtarid=" + _resultid
                  + "&mplan=" + _planType);
        }
        else if (_companyId == "36") {
            string _formURL = CE.Configuration.MyConfigurationManager.GetAppSettingValue("Mobile.ACEForm");

            Response.Redirect(_formURL + "?mtaid=" + _taid + "&mpid=" + _companyId + "&muid=" + _userid + "&mtarid=" + _resultid
                  + "&mplan=" + _planType);
        }

    }

    private void LogUserSelectHistory(string CoverType, Int64 ResultId, Int64 CompanyId)
    {
        UserSelectHistoryDB _userselectdb = new UserSelectHistoryDB();
        if (CoverType.ToLower() == "ind")
        {
            _userselectdb.LogUserSelectHistory("TA", Request.Browser.Type, Request.UserHostAddress, "TAIND", ResultId, CompanyId);
        }
        else if (CoverType.ToLower() == "ins")
        {
            _userselectdb.LogUserSelectHistory("TA", Request.Browser.Type, Request.UserHostAddress, "TAINDSP", ResultId, CompanyId);
        }
        else
        {
            _userselectdb.LogUserSelectHistory("TA", Request.Browser.Type, Request.UserHostAddress, "TAFML", ResultId, CompanyId);
        }

    }

    private void TAUserOptionLog(bool isBuyNow, bool isCallMe, bool isEmailMe)
    {
        TAUserOption tauseroption = new TAUserOption();
        TAUserOptionDB tauseroptiondb = new TAUserOptionDB();

        tauseroption.TAUserOptionId = 0;
        tauseroption.TAId = Convert.ToInt64(Session[General.TAId].ToString());
        tauseroption.IsBuyNow = isBuyNow;
        tauseroption.IsCallMe = isCallMe;
        tauseroption.IsEmailMe = isEmailMe;
        tauseroption.IsEmailThis = false;
        tauseroption.RecipientName = "";
        tauseroption.RecipientEmailAddress = "";
        tauseroption.ViewedDetailId = 0;
        tauseroption.RefCEUserId = 0;

        if (Session[General.UserId] != null)
        {
            if (Session[General.UserId].ToString() != "")
                tauseroption.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
        }

        tauseroptiondb.Insert(tauseroption);
    }
}
